

import java.util.ArrayList;
import java.net.InetAddress;

public class PlayerTable {
    
    private static PlayerTable pt;
    private static ArrayList<Player> players;
    
    private PlayerTable()
    {
        players = new ArrayList<Player>();
    }
    
    public static synchronized PlayerTable getInstance()
    {
        if(pt == null)
            pt = new PlayerTable();
        return pt;
    }
	
	public static synchronized ArrayList<Player> getPlayers()
	{
		return players;
	}
    
    public synchronized boolean addPlayer(String username, InetAddress ip)
    {
        if(!isLoggedIn(username))
        {
            players.add(new Player(username, ip));
            return true;
        }
        return false;
    }
    
    public synchronized boolean addPlayer(Player newPlayer)
    {
        if(!isLoggedIn(newPlayer.getName()))
        {
            players.add(newPlayer);
            return true;
        }
        return false;
    }
    
    public synchronized boolean removePlayer(String username)
    {
        for(int i = 0; i < players.size(); i++)
        {
            if(players.get(i).getName().equalsIgnoreCase(username))
            {
                players.remove(i);
                return true;
            }
        }
        return false;
    }
    
    public synchronized boolean isLoggedIn(String username)
    {
        for(Player currPlayer : players)
        {
            if(currPlayer.getName().equalsIgnoreCase(username))
                return true;
        }
        return false;
    }
                
    
    public synchronized int getSize()
    {
        return players.size();
    }
    
    public synchronized Player getPlayer(int index)
    {
        return players.get(index);
    }
}
    
    
    
